package com.example.JcGQS.service.impl;


import com.example.JcGQS.WMS.WMSClient;
import com.example.JcGQS.dao.*;
import com.example.JcGQS.enums.InvoiceStateEnum;
import com.example.JcGQS.enums.PurchaseStateEnum;
import com.example.JcGQS.exception.CustomException;
import com.example.JcGQS.pojo.dto.*;
import com.example.JcGQS.pojo.entity.*;
import com.example.JcGQS.pojo.vo.*;
import com.example.JcGQS.service.PurchaseService;
import com.example.JcGQS.util.JwtUtil;
import com.example.JcGQS.util.OrderUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLOutput;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 末日
 * @ClassName PurchaseServiceImpl
 * @Description 采购信息表 服务实现类
 * @date 2023年11月13日
 */
@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;
    @Autowired
    private InvoiceDao invoiceDao;
    @Autowired
    private AuditRelationshipDao auditRelationshipDao;
    @Autowired
    private HrmsDao hrmsDao;
    @Autowired
    WMSClient wmsClient;

    /**
     * 新增采购信息
     */
    @Override
    public void insertPurchase(InsertPurchaseDTO insertPurchaseDTO, Integer userId) {
        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        Purchase purchase = new Purchase();
        // System.out.println(insertPurchaseDTO);
        // System.out.println("********");
        BeanUtils.copyProperties(insertPurchaseDTO, purchase);
        purchase.setPurchasePurchaseUserId(insertPurchaseDTO.getPurchasePurchaseUserId());
        String order = "CG" + OrderUtil.createOrderNumber();
        purchase.setPurchaseOrder(order);
        purchase.setPurchaseSubmitUserId(userId);
        purchase.setPurchaseSubmitTime(LocalDateTime.now());
        purchase.setPurchaseState(PurchaseStateEnum.TO_BE_SUBMITTED.getValue());
        //  System.out.println(purchase);
        purchaseDao.insertPurchase(purchase);
        Purchase purchase1 = purchaseDao.PurchaseOrder(order);
        List<Integer> auditUserIds = insertPurchaseDTO.getAuditUserIds();
        AuditRelationship auditRelationship = new AuditRelationship();
        for (Integer auditUserId : auditUserIds) {
            auditRelationship.setAuditRelationshipUserId(auditUserId);
            auditRelationship.setAuditRelationshipState(PurchaseStateEnum.REVIEWEDING.getValue());
            auditRelationship.setPurchaseId(purchase1.getPurchaseId());
            auditRelationshipDao.auditRelationshipInsert(auditRelationship);
        }

    }

    /**
     * 批量新增采购信息
     */
    @Override
    public void insertPurchaseList(InsertPurchaseListDTO insertPurchaseListDTO, Integer userId) {

        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        for (InsertPurchaseDTO insertPurchaseDTO : insertPurchaseListDTO.getList()) {
            Purchase purchase = new Purchase();
            // System.out.println(insertPurchaseDTO);
            // System.out.println("********");
            BeanUtils.copyProperties(insertPurchaseDTO, purchase);
            purchase.setPurchasePurchaseUserId(insertPurchaseDTO.getPurchasePurchaseUserId());
            String order = "CG" + OrderUtil.createOrderNumber();
            purchase.setPurchaseOrder(order);
            purchase.setPurchaseSubmitUserId(userId);
            purchase.setPurchaseSubmitTime(LocalDateTime.now());
            purchase.setPurchaseState(PurchaseStateEnum.TO_BE_SUBMITTED.getValue());
            //  System.out.println(purchase);
            purchaseDao.insertPurchase(purchase);
            Purchase purchase1 = purchaseDao.PurchaseOrder(order);
            List<Integer> auditUserIds = insertPurchaseDTO.getAuditUserIds();
            AuditRelationship auditRelationship = new AuditRelationship();
            for (Integer auditUserId : auditUserIds) {
                auditRelationship.setAuditRelationshipUserId(auditUserId);
                auditRelationship.setAuditRelationshipState(PurchaseStateEnum.REVIEWEDING.getValue());
                auditRelationship.setPurchaseId(purchase1.getPurchaseId());
                auditRelationshipDao.auditRelationshipInsert(auditRelationship);
            }
        }
    }

    /**
     * 删除采购信息
     */
    @Override
    public void deletePurchase(PurchaseId purchaseId, String token) {

        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        //申购人id跟userid是否相同
        Integer integer1 = purchaseDao.PurchaseSelect(purchaseId.getPurchaseId());
        //  System.out.println(integer1);
        if (integer1 != 0) {
            Purchase purchase = purchaseDao.PurchaseSelectId(purchaseId.getPurchaseId());
            if (userId.equals(purchase.getPurchaseSubmitUserId())) {
                log.info("删除的采购数据为:{}", purchase);
                purchaseDao.deletePurchase(purchaseId.getPurchaseId());
            }
//            if (Objects.isNull(purchase.getPurchaseInvoiceId())) {
//                invoiceDao.deleteInvoice(purchase.getPurchaseInvoiceId());
//            }
//            //审核人
//            auditRelationshipDao.auditRelationshipDelete(purchaseId.getPurchaseId());

        } else {
            throw new CustomException("该采购信息不存在");
        }
    }

    /**
     * 更新采购信息
     */
    @Override
    public void updatePurchase(UpdatePurchaseDTO updatePurchaseDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = purchaseDao.PurchaseSelect(updatePurchaseDTO.getPurchaseId());
        Purchase purchase = new Purchase();
        //有这条采购信息
        if (integer != 0) {
            Purchase purchase1 = purchaseDao.PurchaseSelectId(updatePurchaseDTO.getPurchaseId());
            //  System.out.println(updatePurchaseDTO.getPurchasePayQuantily());
            //  System.out.println(updatePurchaseDTO.getPurchasePayQuantily());
            if (!Objects.isNull(updatePurchaseDTO.getPurchasePayQuantily()) && updatePurchaseDTO.getPurchasePayQuantily() < purchase1.getPurchaseSubmitQuantity()) {
                throw new CustomException("实际购买物品数量不能小于申请采购物品数量!!!");
            }
//            //有无需要新增修改的发票数据
//            if (!Objects.isNull(updatePurchaseDTO.getInvoiceNumber()) || !Objects.isNull(updatePurchaseDTO.getInvoicesRate()) || !Objects.isNull(updatePurchaseDTO.getInvoicesTaxes()) || !Objects.isNull(updatePurchaseDTO.getInvoicesType()) || !Objects.isNull(updatePurchaseDTO.getInvoiceState())) {
//                Invoice invoice = new Invoice();
//                BeanUtils.copyProperties(updatePurchaseDTO, invoice);
//                System.out.println(invoice);
//                invoiceDao.InvoiceUpdate(updatePurchaseDTO.getPurchaseInvoiceId(), invoice);
//            }
            //个人领取数量不为null
            if (!Objects.isNull(updatePurchaseDTO.getPersonalNumber())) {
                if (updatePurchaseDTO.getPersonalNumber() + purchase1.getPersonalNumber() + purchase1.getNumberProjects() + purchase1.getQuantityStorage() > purchase1.getPurchasePayQuantily()) {
                    throw new CustomException("个人领取数量  +项目领取数量+入库数量 大于 了实际采购数量!!");
                }
                PersonalUseDTO personalUseDTO = new PersonalUseDTO();
                personalUseDTO.setIndividualOutputGoodsId(purchase1.getPurchaseGoodsId());
                personalUseDTO.setIndividualOutputQuantity(updatePurchaseDTO.getPersonalNumber());
                personalUseDTO.setIndividualOutputRemark("采购系统个人直接领取");
                personalUseDTO.setUserId(userId);
                personalUseDTO.setSubmitUserId(purchase1.getPurchaseSubmitUserId());
                //添加仓库系统里的个人出库记录
                wmsClient.personalUse(personalUseDTO);

                updatePurchaseDTO.setPersonalNumber(updatePurchaseDTO.getPersonalNumber() + purchase1.getPersonalNumber());

            }
            //项目领取数量不为null
            if (!Objects.isNull(updatePurchaseDTO.getNumberProjects())) {
                if (updatePurchaseDTO.getNumberProjects() + purchase1.getNumberProjects() + purchase1.getPersonalNumber() + purchase1.getQuantityStorage() > purchase1.getPurchasePayQuantily()) {
                    throw new CustomException("项目领取数量  +个人领取数量+入库数量 大于 了实际采购数量!!");
                }
                System.out.println(purchase1.getPurchaseProjectId());
                ProjectAcceptanceDTO projectAcceptanceDTO =
                        new ProjectAcceptanceDTO(purchase1.getPurchaseGoodsId(),
                                purchase1.getPurchaseProjectId(),
                                updatePurchaseDTO.getNumberProjects(),
                                "采购系统项目直接领用",
                                userId, purchase1.getPurchaseSubmitUserId());
                //添加仓库系统里的项目出库记录
                wmsClient.projectAcceptance(projectAcceptanceDTO);
                updatePurchaseDTO.setNumberProjects(updatePurchaseDTO.getNumberProjects() + purchase1.getNumberProjects());

            }
            //入库数量不为null
            if (!Objects.isNull(updatePurchaseDTO.getQuantityStorage())) {
                if (updatePurchaseDTO.getQuantityStorage() + purchase1.getQuantityStorage() + purchase1.getPersonalNumber() + purchase1.getNumberProjects() > purchase1.getPurchasePayQuantily()) {
                    throw new CustomException("入库数量  +个人领取数量+项目领取数量 大于 了实际采购数量!!");
                }
                PutInStorageDTO putInStorageDTO = new PutInStorageDTO(
                        purchase1.getPurchaseGoodsId(),
                        updatePurchaseDTO.getQuantityStorage(),
                        1,
                        "采购系统直接入库",
                        userId);
                //仓库系统入库
                wmsClient.putInStorage(putInStorageDTO);

                updatePurchaseDTO.setQuantityStorage(updatePurchaseDTO.getQuantityStorage() + purchase1.getQuantityStorage());

            }
//            System.out.println(updatePurchaseDTO.getAuditUserIds());
            List<Integer> auditUserIds = updatePurchaseDTO.getAuditUserIds();
            //审核人数组不为null修改审核人数组
            if (!Objects.isNull(auditUserIds)) {
                //先删除原来的审核人数据
                auditRelationshipDao.auditRelationshipDelete(updatePurchaseDTO.getPurchaseId());
                AuditRelationship auditRelationship = new AuditRelationship();
                for (Integer auditUserId : auditUserIds) {
                    auditRelationship.setAuditRelationshipUserId(auditUserId);
                    auditRelationship.setAuditRelationshipState(PurchaseStateEnum.REVIEWEDING.getValue());
                    auditRelationship.setPurchaseId(updatePurchaseDTO.getPurchaseId());
                    //新增审核人数据
                    auditRelationshipDao.auditRelationshipInsert(auditRelationship);
                }
            }
            Invoice invoice = new Invoice();
            Purchase purchase2 = new Purchase();
            if (Objects.isNull(purchase1.getPurchaseInvoiceId())) {

                String order = "FP" + OrderUtil.createOrderNumber();
                invoice.setInvoiceOrder(order);
                if (!Objects.isNull(updatePurchaseDTO.getInvoiceNumber())) {
                    invoice.setInvoiceNumber(updatePurchaseDTO.getInvoiceNumber());
                }
                if (!Objects.isNull(updatePurchaseDTO.getInvoicesRate())) {
                    invoice.setInvoicesRate(updatePurchaseDTO.getInvoicesRate());
                }
                if (!Objects.isNull(updatePurchaseDTO.getInvoiceState())) {
                    invoice.setInvoiceState(updatePurchaseDTO.getInvoiceState());
                }
//                else if (!Objects.isNull(updatePurchaseDTO.getInvoiceNumber())
//                || !Objects.isNull(updatePurchaseDTO.getInvoicesRate())
//                || !Objects.isNull(updatePurchaseDTO.getInvoicesTaxes())
//                || !Objects.isNull(updatePurchaseDTO.getInvoicesType())){
//                    invoice.setInvoiceState("有");
//                }
                else{
                    invoice.setInvoiceState("无");
                }
                if (!Objects.isNull(updatePurchaseDTO.getInvoicesTaxes())) {
                    invoice.setInvoicesTaxes(updatePurchaseDTO.getInvoicesTaxes());
                }
                if (!Objects.isNull(updatePurchaseDTO.getInvoicesType())) {
                    invoice.setInvoicesType(updatePurchaseDTO.getInvoicesType());
                }

                invoiceDao.InvoiceInsert(invoice);
                Invoice invoice1 = invoiceDao.InvoiceSelect(order);

                purchase2.setPurchaseId(updatePurchaseDTO.getPurchaseId());
                purchase2.setPurchaseInvoiceId(invoice1.getInvoiceId());
                purchaseDao.updatePurchase(purchase2);
            }
            if (!Objects.isNull(updatePurchaseDTO.getInvoiceNumber()) || !Objects.isNull(updatePurchaseDTO.getInvoicesRate()) || !Objects.isNull(updatePurchaseDTO.getInvoicesTaxes()) || !Objects.isNull(updatePurchaseDTO.getInvoicesType()) || !Objects.isNull(updatePurchaseDTO.getInvoiceState())) {
                BeanUtils.copyProperties(updatePurchaseDTO, invoice);
                invoiceDao.InvoiceUpdate(updatePurchaseDTO.getPurchaseInvoiceId(), invoice);
            }
            BeanUtils.copyProperties(updatePurchaseDTO, purchase);
            //总价不为null算出单价
            if (!Objects.isNull(updatePurchaseDTO.getPurchaseTotallPrice())) {
                BigDecimal decimalValue = new BigDecimal(updatePurchaseDTO.getPurchaseTotallPrice());
                BigDecimal decimalValue1 = new BigDecimal(updatePurchaseDTO.getPurchasePayQuantily());
                BigDecimal quotient = decimalValue.divide(decimalValue1, 2, BigDecimal.ROUND_HALF_UP); // 保留两位小数
                //单价
                purchase.setPurchaseUnitprice(quotient.toString());
            }
            //修改采购状态
            purchaseDao.updatePurchase(purchase);
        } else {
            throw new CustomException("该采购信息不存在");
        }
    }

    @Override
    public void aVoid() {
//
//        PutInStorageDTO putInStorageDTO = new PutInStorageDTO(
//                29,
//                100,
//                1,
//                "采购系统直接入库",
//                1
//        );
//        //仓库系统入库
//        wmsClient.putInStorage(putInStorageDTO);
//
//        ProjectAcceptanceDTO projectAcceptanceDTO =
//                new ProjectAcceptanceDTO(29,
//                        10,
//                        100,
//                        "采购系统项目直接领用",
//                        1,86);
//        //添加仓库系统里的项目出库记录
//
//        wmsClient.projectAcceptance(projectAcceptanceDTO);
//
//        PersonalUseDTO personalUseDTO = new PersonalUseDTO();
//        personalUseDTO.setIndividualOutputGoodsId(29);
//        personalUseDTO.setIndividualOutputQuantity(100);
//        personalUseDTO.setIndividualOutputRemark("采购系统个人直接领取");
//        personalUseDTO.setUserId(1);
//        personalUseDTO.setSubmitUserId(86);
//        //添加仓库系统里的个人出库记录
//        wmsClient.personalUse(personalUseDTO);

    }

    /**
     * 更新采购状态
     */
    @Override
    public void updatePurchaseState(UpdatePurchaseStateDTO updatePurchaseStateDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = purchaseDao.PurchaseSelect(updatePurchaseStateDTO.getPurchaseId());
        if (integer != 0) {
            Purchase purchase1 = purchaseDao.PurchaseSelectId(updatePurchaseStateDTO.getPurchaseId());
            Purchase purchase = new Purchase();
            BeanUtils.copyProperties(updatePurchaseStateDTO, purchase);
            //修改为已下单状态时必须填上总价与实际采购数量
            if (updatePurchaseStateDTO.getPurchaseState().equals(PurchaseStateEnum.HAVE_ORDER.getValue())) {
                if (!Objects.isNull(purchase1.getPurchasePayQuantily()) && !Objects.isNull(purchase1.getPurchaseTotallPrice()) && !Objects.isNull(purchase1.getPurchaseSupplierId())) {
                    purchase.setPurchasePurchaseTime(LocalDateTime.now());
                } else {
                    throw new CustomException("总价,实际采购数量与供应商需要必填!!!");
                }
                if (purchase1.getPurchasePayQuantily() < purchase1.getPurchaseSubmitQuantity()) {
                    throw new CustomException("实际购买物品数量不能小于申请采购物品数量!!!");
                }
            }
            if (!updatePurchaseStateDTO.getPurchaseState().equals(PurchaseStateEnum.NORegenerate.getValue()) && !updatePurchaseStateDTO.getPurchaseState().equals(PurchaseStateEnum.Regenerate.getValue())) {
                purchaseDao.updatePurchase(purchase);
            }
            if (purchase1.getPurchaseState().equals(PurchaseStateEnum.PURCHASE_FAILURE.getValue()) || purchase1.getPurchaseState().equals(PurchaseStateEnum.NOT_ALLOW.getValue())) {
                //如果是采购失败或审核失败
                if (updatePurchaseStateDTO.getPurchaseState().equals(PurchaseStateEnum.Regenerate.getValue())) {
                    // 如果重新生成新数据
                    InsertPurchaseDTO insertPurchaseDTO = new InsertPurchaseDTO();
                    insertPurchaseDTO.setPurchaseGoodsId(purchase1.getPurchaseGoodsId());
                    insertPurchaseDTO.setPurchaseRemark(purchase1.getPurchaseRemark());
                    insertPurchaseDTO.setPurchaseProjectId(purchase1.getPurchaseProjectId());
                    insertPurchaseDTO.setPurchaseSubmitQuantity(purchase1.getPurchaseSubmitQuantity());
                    insertPurchaseDTO.setPurchasePurchaseUserId(purchase1.getPurchasePurchaseUserId());
                    List<Integer> list = new ArrayList<>();
                    List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchase1.getPurchaseId());
                    for (AuditRelationship relationship : auditRelationships) {
                        list.add(relationship.getAuditRelationshipUserId());
                    }

                    insertPurchaseDTO.setAuditUserIds(list);
                    this.insertPurchase(insertPurchaseDTO, userId);
                }
                //如果不生成新数据
                Purchase purchase2 = new Purchase();
                purchase2.setPurchaseId(updatePurchaseStateDTO.getPurchaseId());
                if (purchase1.getPurchaseState().equals(PurchaseStateEnum.PURCHASE_FAILURE.getValue())) {
                    purchase2.setPurchaseState(PurchaseStateEnum.PURCHASE_FAILURE_DELETE.getValue());
                }
                if (purchase1.getPurchaseState().equals(PurchaseStateEnum.NOT_ALLOW.getValue())) {
                    purchase2.setPurchaseState(PurchaseStateEnum.NOT_ALLOW_DELETE.getValue());
                }
                purchaseDao.updatePurchase(purchase2);
                PurchaseId purchaseId = new PurchaseId();
                purchaseId.setPurchaseId(updatePurchaseStateDTO.getPurchaseId());
                this.deletePurchase(purchaseId, token);

            }
        } else {
            throw new CustomException("该采购信息不存在");
        }

    }


    /**
     * 查询采购信息列表
     */
    @Override
    public DataRecordVo<Purchase> PurchaseList(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);

        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<Purchase> PurchaseCategories = purchaseDao.PurchaseList();
//        System.out.println(PurchaseCategories);
        PageInfo<Purchase> pageInfo = new PageInfo<>(PurchaseCategories);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());

    }


    /**
     * 根据申购人查询(审核中/已下单/采购中/采购完成)采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> applicantReviewedingPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.applicantReviewedingPurchase(userId);

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据申购人查询(采购失败并删除/审核失败并删除)采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> applicantDeletePurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.applicantDeletePurchase(userId);
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());

    }

    /**
     * 根据申购人查询(待提交)采购信息
     */
    @Override
    public DataRecordVo<ApplicantToSubmitVo> applicantToSubmitPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.applicantToSubmitPurchase(userId);
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        List<ApplicantToSubmitVo> applicantToSubmitVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchaseVos) {
            ApplicantToSubmitVo applicantToSubmitVo = new ApplicantToSubmitVo();
            BeanUtils.copyProperties(purchaseVo, applicantToSubmitVo);
            applicantToSubmitVos.add(applicantToSubmitVo);
        }

        PageInfo<ApplicantToSubmitVo> pageInfo = new PageInfo<>(applicantToSubmitVos);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());


    }

    /**
     * 根据申购人查询(采购失败/审核失败)采购信息
     */
    @Override
    public DataRecordVo<ApplicantToSubmitVo> applicantToSubmitPurchaseLose(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "purchase_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.applicantToSubmitPurchaseLose(userId);
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        List<ApplicantToSubmitVo> applicantToSubmitVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchaseVos) {
            ApplicantToSubmitVo applicantToSubmitVo = new ApplicantToSubmitVo();
            BeanUtils.copyProperties(purchaseVo, applicantToSubmitVo);
            applicantToSubmitVos.add(applicantToSubmitVo);
        }
        PageInfo<ApplicantToSubmitVo> pageInfo = new PageInfo<>(applicantToSubmitVos);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据审核人查询(审核中)采购信息
     */
    @Override
    public DataRecordVo<AuditReviewedingVo> auditReviewedingPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "audit_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.auditReviewedingPurchase(userId);

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        List<AuditReviewedingVo> applicantToSubmitVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchaseVos) {
            AuditReviewedingVo applicantToSubmitVo = new AuditReviewedingVo();
            BeanUtils.copyProperties(purchaseVo, applicantToSubmitVo);
            applicantToSubmitVos.add(applicantToSubmitVo);
        }
        PageInfo<AuditReviewedingVo> pageInfo = new PageInfo<>(applicantToSubmitVos);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据审核人查询(审核失败/采购中/已下单/采购完成/采购失败/采购失败并删除/审核失败并删除)采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> auditUnderPurchasePurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "audit_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.auditUnderPurchasePurchase(userId);

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据采购人来查询(审核中)采购信息
     */
    @Override
    public DataRecordVo<AuditReviewedingVo> buyerReviewedingPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.buyerReviewedingPurchase(userId);
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        List<AuditReviewedingVo> applicantToSubmitVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchaseVos) {
            AuditReviewedingVo applicantToSubmitVo = new AuditReviewedingVo();
            BeanUtils.copyProperties(purchaseVo, applicantToSubmitVo);
            applicantToSubmitVos.add(applicantToSubmitVo);
        }
        PageInfo<AuditReviewedingVo> pageInfo = new PageInfo<>(applicantToSubmitVos);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据采购人来查询(采购中/已下单)采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> buyerUnderPurchasePurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.buyerUnderPurchasePurchase(userId);

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据采购人来查询(采购完成)采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> buyerPurchaseCompletedPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.buyerPurchaseCompletedPurchase(userId);

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据采购人来查询(采购失败/采购失败并删除)采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> buyerPurchaseFailedPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "procurement_authority");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.buyerPurchaseFailedPurchase(userId);

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 根据仓库管理人来查询(已下单)的采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> warehousemanHaveOrderPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "warehouse_administrator");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.warehousemanHaveOrderPurchase();

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());

    }


    /**
     * 根据仓库管理人来查询(退货中)的采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> warehousemanReturnPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "warehouse_administrator");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.warehousemanReturnPurchase();

        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));

                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
//return null;
    }

    /**
     * 根据仓库管理人来查询(采购完成)的采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> warehousemanPurchaseCompletedPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "warehouse_administrator");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.warehousemanPurchaseCompletedPurchase();
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));
                BeanUtils.copyProperties(invoice, purchaseVo);

            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
//return null;
    }


    /**
     * 根据仓库管理人来查询(采购失败/采购师并删除)的采购信息
     */
    @Override
    public DataRecordVo<PurchaseVo> warehousemanPurchaseFailedPurchase(PageDTO pageDTO, String token) {
        Integer userId = JwtUtil.getUserIdByJWT(token);
        Integer integer = hrmsDao.permissionSelect(userId, "warehouse_administrator");
        if (integer == 0) {
            //无权限
            throw new CustomException("暂无该权限!!");
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getNum());
        List<PurchaseVo> purchases = purchaseDao.warehousemanPurchaseFailedPurchase();
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }
            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));

            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                invoice.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(invoice.getInvoicesType()));
                BeanUtils.copyProperties(invoice, purchaseVo);

            }
            purchaseVos.add(purchaseVo);
        }
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchases);
        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());

    }

    /**
     * 根据采购信息查询所有审核人
     */

    @Override
    public void auditListPurchase(PageDTO pageDTO, String token) {
        //  Integer userId = JwtUtil.getUserIdByJWT(token);
    }


//    @Override
//    public DataRecordVo<PurchaseVo> a(PageADTO pageADTO, String token) {
//        //  Integer userId = JwtUtil.getUserIdByJWT(token);
//        Integer userId = JwtUtil.getUserIdByJWT(token);
//
//
//        PageHelper.startPage(pageADTO.getPage(), pageADTO.getNum());
//        List<PurchaseVo> purchases = purchaseDao.a(pageADTO,userId);
//
//        System.out.println(purchases);
//        List<PurchaseVo> purchaseVos = new ArrayList<>();
//        for (PurchaseVo purchaseVo : purchases) {
//            System.out.println("======2======");
//            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
//            System.out.println("======3======" + auditRelationships);
//            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
//            for (AuditRelationship auditRelationship : auditRelationships) {
//                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
//                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
//                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
//                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
//                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
//                auditRelationshipVos.add(auditRelationshipVo);
//            }
//            purchaseVo.setAudits(auditRelationshipVos);
//            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
//            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
//            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
//                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
//            }
//            //TODO 插入虚假项目名称
//            purchaseVo.setPurchaseProjectName(ProjectStateEnum.getDescriptionByCode(purchaseVo.getPurchaseProjectId()));
//
//            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
//            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
//            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
//            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
//                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
//                BeanUtils.copyProperties(invoice, purchaseVo);
//            }
//            purchaseVos.add(purchaseVo);
//        }
//        PageInfo<PurchaseVo> pageInfo = new PageInfo<>(purchaseVos);
//        return new DataRecordVo<>(pageInfo.getTotal(), pageInfo.getList());
//
//    }

    public List<PurchaseVo> aVoid(List<PurchaseVo> purchases) {
        List<PurchaseVo> purchaseVos = new ArrayList<>();
        for (PurchaseVo purchaseVo : purchases) {
            List<AuditRelationship> auditRelationships = auditRelationshipDao.auditRelationshipSelectId(purchaseVo.getPurchaseId());
            List<AuditRelationshipVo> auditRelationshipVos = new ArrayList<>();
            for (AuditRelationship auditRelationship : auditRelationships) {
                AuditRelationshipVo auditRelationshipVo = new AuditRelationshipVo();
                BeanUtils.copyProperties(auditRelationship, auditRelationshipVo);
                auditRelationshipVo.setAuditRelationshipState(PurchaseStateEnum.getDescriptionByCode(auditRelationshipVo.getAuditRelationshipState()));
                Employee employee = hrmsDao.nameSelect(auditRelationship.getAuditRelationshipUserId());
                auditRelationshipVo.setAuditRelationshipUserName(employee.getEmployeeName());
                auditRelationshipVos.add(auditRelationshipVo);
            }
            purchaseVo.setAudits(auditRelationshipVos);
            purchaseVo.setPurchaseState(PurchaseStateEnum.getDescriptionByCode(purchaseVo.getPurchaseState()));
            Employee employee = hrmsDao.nameSelect(purchaseVo.getPurchasePurchaseUserId());
            purchaseVo.setPurchasePurchaseUserName(employee.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getInvoicesType())) {
                purchaseVo.setInvoicesType(InvoiceStateEnum.getDescriptionByCode(purchaseVo.getInvoicesType()));
            }

            purchaseVo.setPurchaseProjectName(hrmsDao.nameProjectSelect(purchaseVo.getPurchaseProjectId()));
            Employee employee1 = hrmsDao.nameSelect(purchaseVo.getPurchaseSubmitUserId());
            purchaseVo.setPurchaseSubmitUserName(employee1.getEmployeeName());
            if (!Objects.isNull(purchaseVo.getPurchaseInvoiceId())) {
                Invoice invoice = invoiceDao.InvoiceSelectId(purchaseVo.getPurchaseInvoiceId());
                BeanUtils.copyProperties(invoice, purchaseVo);
            }
            purchaseVos.add(purchaseVo);
        }
        return purchaseVos;
    }

}
